﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using ExamAnalysis.Model;

namespace ExamAnalysis.Core
{
    public static class InputUtil
    {
        public static int inputExamFromExcel(FileInfo excelFile)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            DataTable dataTable = ExcelUtil.ReadExcelToTable(excelFile);
            ExamInfo newExam = null;

            int row = 2;
            int insterNum = 0;
            while (row < dataTable.Rows.Count)
            {
                newExam = new ExamInfo()
                {
                    ExamName = dataTable.Rows[row][0].ToString()
                };
                db.ExamInfo.InsertOnSubmit(newExam);
                insterNum++;
                row++;
            }
            db.SubmitChanges();
            return insterNum;

        }

        public static int inputAnswersFromExcel(FileInfo excelFile, int stuid)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            DataTable dataTable = ExcelUtil.ReadExcelToTable(excelFile);
            StudentInExamItem newClass = null;

            int row = 2;
            int insterNum = 0;
            while (row < dataTable.Rows.Count)
            {
                newClass = new StudentInExamItem()
                {
                    ExamItemID =int.Parse( dataTable.Rows[row][0].ToString()),
                    StudentID=stuid,
                    Score = int.Parse(dataTable.Rows[row][3].ToString())
                };
                db.StudentInExamItem.InsertOnSubmit(newClass);
                insterNum++;
                row++;
            }
            db.SubmitChanges();
            return insterNum;
        }



        public static int inputClassFromExcel(FileInfo excelFile, int schoolID)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            DataTable dataTable = ExcelUtil.ReadExcelToTable(excelFile);
            ClassInfo newClass = null;

            int row = 2;
            int insterNum = 0;
            while (row < dataTable.Rows.Count)
            {
                newClass = new ClassInfo()
                {
                    ClassCode = dataTable.Rows[row][0].ToString(),
                    ClassName = dataTable.Rows[row][1].ToString(),
                    ClassType = dataTable.Rows[row][2].ToString(),
                    ClassAliasName = dataTable.Rows[row][3].ToString(),
                    ClassGrade = dataTable.Rows[row][4].ToString(),
                    EnrollYear = Int16.Parse(dataTable.Rows[row][5].ToString()),
                    SchoolID = schoolID
                };
                db.ClassInfo.InsertOnSubmit(newClass);
                insterNum++;
                row++;
            }
            db.SubmitChanges();
            return insterNum;
        }

        public static int inputExamItemsFromExcel(FileInfo excelFile, int itemGroupID)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            DataTable dataTable = ExcelUtil.ReadExcelToTable(excelFile);
            ExamItems newItem = null;

            int row = 2;
            int insterNum = 0;
            while (row < dataTable.Rows.Count)
            {
                newItem = new ExamItems()
                {
                    ItemName = dataTable.Rows[row][2].ToString(),
                    ItemScore = int.Parse(dataTable.Rows[row][1].ToString()),
                    ItemType = dataTable.Rows[row][0].ToString(),
                    DifficultyLevel = int.Parse(dataTable.Rows[row][3].ToString()),
                    ExamItemGroupID = itemGroupID
                };
                db.ExamItems.InsertOnSubmit(newItem);
                insterNum++;
                row++;
            }
            db.SubmitChanges();
            return insterNum;
        }

        public static int inputStudentFromExcel(FileInfo excelFile, int classId)
        {
            StudentInfo student = null;
            int row = 2;
            int insterNum = 0;
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                DataTable dataTable = ExcelUtil.ReadExcelToTable(excelFile, "学生信息录入");
                while (row < dataTable.Rows.Count)
                {
                    student = new StudentInfo()
                    {
                        StudentCode = dataTable.Rows[row][0].ToString(),
                        StudentName = dataTable.Rows[row][1].ToString(),
                        Sex = dataTable.Rows[row][2].ToString(),
                        StudentType = dataTable.Rows[row][3].ToString(),
                        ClassID = classId
                    };
                    insterNum++;
                    row++;
                    db.StudentInfo.InsertOnSubmit(student);

                }
                db.SubmitChanges();
                return insterNum;
            }
        }


        public static int inputSubjectFromExcel(FileInfo excelFile, int schoolId)
        {
            SubjectInfo subInfo = null;
            int row = 2;
            int insterNum = 0;
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                DataTable dataTable = ExcelUtil.ReadExcelToTable(excelFile, "科目信息录入");
                while (row < dataTable.Rows.Count)
                {
                    subInfo = new SubjectInfo()
                    {
                        SubjectCode = dataTable.Rows[row][0].ToString(),
                        SubjectName = dataTable.Rows[row][1].ToString(),
                        SchoolID = schoolId
                    };
                    insterNum++;
                    row++;
                    db.SubjectInfo.InsertOnSubmit(subInfo);

                }
                db.SubmitChanges();
                return insterNum;
            }
        }
    }
}
